package com.gree.flowable.app.controller;

import com.gree.flowable.app.pojo.ex.ApplySubmitPO;
import com.gree.flowable.app.pojo.to.IFormImportExcelTO;
import com.gree.flowable.app.pojo.vo.ReturnVO;
import com.gree.flowable.app.services.IFormImportExcelService;
import com.gree.flowable.app.tools.constants.ReturnCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author ：180849
 * @date ：Created in 2021/5/11 11:27
 * @description：批量创建单据的Excel导入
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("/rest")
public class IFormImportExcelApi {


    @Autowired
    private IFormImportExcelService iFormImportExcelService;

    @RequestMapping(value = "/importFormExcel",method = RequestMethod.GET,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public ReturnVO<List<ApplySubmitPO>> importFormExcel(@RequestParam("processKey")String processKey,
                                                         @RequestParam("formId")String formId,
//            @RequestBody Map<String,String> params,
                                                         @RequestPart("file")MultipartFile file) throws Exception {
//        String processKey = params.get("processKey");
//        String formId = params.get("formId");
        List<ApplySubmitPO> applySubmitPOList = iFormImportExcelService.importForm(file,  formId, processKey);
        ReturnVO<List<ApplySubmitPO>> result = null;
        if (applySubmitPOList.size() > 0){
            result = new ReturnVO<>(ReturnCode.SUCCESS,"数据解析成功",applySubmitPOList);
        } else {
            result = new ReturnVO<>(ReturnCode.FAIL,"数据解析失败",applySubmitPOList);
        }
        return result;
    }

    @PostMapping("/getExcelHeard")
    @ResponseBody
    public ReturnVO<Map<String,Object>> getExcelHeard(@RequestBody IFormImportExcelTO iFormImportExcelTO){
        String formId = iFormImportExcelTO.getFormId();
        String processKey = iFormImportExcelTO.getProcessKey();
        return iFormImportExcelService.getExcelHead(formId, processKey);
    }

}
